<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <!-- <el-form-item label="关键字" prop="keyword">
                    <el-input v-model.trim="queryParams.keyword" placeholder="请输入关键字" clearable size="small" style="width: 240px" @keyup.enter.native="handleQuery" />
                </el-form-item> -->
                <el-form-item label="订单号" prop="order_id">
                    <el-input type="textarea" :rows="4" v-model="queryParams.order_id" placeholder="请输入订单号" clearable size="small" style="width: 240px" />
                </el-form-item>
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <!-- <el-form-item label="支付状态" prop="financial_status">
                    <el-select v-model="queryParams.financial_status" placeholder="请选择支付状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in FINANCIAL_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="物流状态" prop="fulfillment_status">
                    <el-select v-model="queryParams.fulfillment_status" placeholder="请选择物流状态" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in FULFILLMENT_STATUS" :key="index" :label="dict.label" :value="dict.value" />
                    </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="争议状态:" prop="is_disputeapi_status">
                        <el-select v-model="queryParams.is_disputeapi_status" placeholder="请选择" clearable>
                            <el-option
                                v-for="item in disputeapi_status_arr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                </el-form-item> -->
                <!-- <el-form-item label="在途状态:" prop="LogisticsStatus">
                        <el-select v-model="queryParams.LogisticsStatus" placeholder="请选择" clearable>
                            <el-option
                                v-for="item in LogisticsStatus_arr"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                </el-form-item> -->

                <el-form-item label="开始时间" prop="createtime">
                    <el-date-picker v-model="queryParams.createtime" format="yyyy-MM-dd" type="daterange" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px"></el-date-picker>
                </el-form-item>
                <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="card">
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button type="primary" plain @click="showFeedbackDialog()">店铺退款率</el-button>
                </el-col>
                <right-toolbar :showSearch.sync="showSearch" :columns="columns" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <!-- <el-table-column label="订单号" prop="order_id" min-width="200" show-overflow-tooltip v-if="showColumn('order_id')"></el-table-column> -->
                <el-table-column label="店铺" prop="shop_name" min-width="200" show-overflow-tooltip v-if="showColumn('shop_name')"></el-table-column>

                <el-table-column label="订单号" prop="order_id" min-width="200" show-overflow-tooltip v-if="showColumn('order_id')">
                    <template slot-scope="scope">
                        <el-link @click="showDetailDialog(scope.row, 'view')">{{ scope.row.order_id }}</el-link>
                    </template>
                </el-table-column>
                
                <el-table-column label="支付通道" prop="payment_gateway_names" min-width="200" show-overflow-tooltip v-if="showColumn('payment_gateway_names')"></el-table-column>
                <el-table-column label="支付状态" prop="financial_status" min-width="100" v-if="showColumn('financial_status')">
                    <template slot-scope="scope">
                        {{ scope.row.financial_status | formatLabelByValueFuzzy(STATUS_LIST) }}
                    </template>
                </el-table-column>
                <el-table-column label="发货状态" prop="fulfillment_status" min-width="100" v-if="showColumn('fulfillment_status')">
                    <template slot-scope="scope">
                        {{ scope.row.fulfillment_status | formatLabelByValueFuzzy(STATUS_LIST) }}
                    </template>
                </el-table-column>
                <!-- <el-table-column label="争议状态" prop="disputeapi_status" min-width="100" v-if="showColumn('disputeapi_status')"></el-table-column> -->
                <el-table-column label="创建时间" prop="createtime" min-width="160" v-if="showColumn('createtime')"></el-table-column>
                <!-- <el-table-column label="操作" min-width="160" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-edit" @click="showFeedbackDialog(scope.row)">标记</el-button>
                    </template>
                </el-table-column> -->
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
        <detail-dialog v-model="detailDialog.visible" v-if="detailDialog.visible" :params="detailDialog.data" :type="detailDialog.type" @success="handleQuery"></detail-dialog>
        <feedback-dialog v-model="feedbackDialog.visible" v-if="feedbackDialog.visible" :params="feedbackDialog.data" @success="handleQuery"></feedback-dialog>
    </div>
</template>

<script>
import { parseTime } from '@/utils/quickadmin'
import { queryOrderRefundList, exportOrderRefund } from '@/api/order'
import { getShopList } from '@/api/shop'
import { STATUS_LIST, FINANCIAL_STATUS, FULFILLMENT_STATUS ,DISPUTEAPI_STATUS} from './constant'
import { exportFile } from '@/utils/index'
import DetailDialog from './components/DetailDialog.vue'
import FeedbackDialog from './components/FeedbackDialog.vue'
export default {
    name: 'OrderList',
    components: { DetailDialog, FeedbackDialog },
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            STATUS_LIST,
            FINANCIAL_STATUS,
            DISPUTEAPI_STATUS,
            FULFILLMENT_STATUS,
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                order_id: undefined,
                shop_id: undefined,
                name: undefined,
                financial_status: undefined,
                fulfillment_status: undefined,
                is_disputeapi_status: undefined,
                createtime: [
                    parseTime(new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)),
                    parseTime(new Date()),
                ],
                keyword: undefined,
            },
            detailDialog: {
                visible: false,
                data: {},
                type: ''
            },
            feedbackDialog: {
                visible: false,
                data: {},
            },
            shopList: [],
            disputeapi_status_arr: [
                    {
                    value: '1',
                    label: '审核中'
                    },
                    {
                    value : '2',
                        label: '争议输',
                    },
                    {
                    value : '3',
                        label: '其他',
                    },
                    {
                    value : '4',
                        label: '待处理',
                    },
                    {
                    value : '5',
                        label: '争议赢',
                    },
            ],

            LogisticsStatus_arr: [
                {
                    value: 'InTransit_Other',
                    label: '在途'
                },
                {
                    value: 'InTransit_PickedUp',
                    label: '已揽收'
                },
                {
                    value: 'InfoReceived',
                    label: '已收到信息'
                },
                {
                    value: 'OutForDelivery_Other',
                    label: '出门投递-其他'
                },
                {
                    value: 'Delivered_Other',
                    label: '已妥投-其他'
                },
                {
                    value: 'InTransit_CustomsReleased',
                    label: '在途-海关放行'
                },
                {
                    value: 'InTransit_CustomsProcessing',
                    label: '在途-海关处理中'
                },
                {
                    value: 'Exception_Delayed',
                    label: '异常-延迟'
                },
                {
                    value: 'AvailableForPickup_Other',
                    label: '到达待取-其它'
                },
                {
                    value: 'Exception_Returning',
                    label: '异常-退回中'
                },
                {
                    value: 'DeliveryFailure_InvalidAddress',
                    label: '投递失败-地址无效'
                },
                {
                    value: 'InTransit_Departure',
                    label: '在途-离境'
                },
                {
                    value: 'DeliveryFailure_Other',
                    label: '投递失败-其它'
                },
                {
                    value: 'DeliveryFailure_NoBody',
                    label: '投递失败-没人'
                },
                {
                    value: 'Exception_Returned',
                    label: '异常-已退回'
                },
                {
                    value: 'Exception_NoBody',
                    label: '异常-没人'
                },
            ],
            columns: [
                {label:'订单号', prop: 'order_id', visible: true },
                {label:'店铺', prop: 'shop_name', visible: true },
                {label:'订单编号', prop: 'name', visible: true },
                {label:'支付通道', prop: 'payment_gateway_names', visible: true },
                {label:'支付状态', prop: 'financial_status', visible: true },
                {label:'物流状态', prop: 'fulfillment_status', visible: true },
                {label:'在途状态', prop: 'LogisticsStatus', visible: true },
                {label:'物流单据', prop: 'tracking_number', visible: true },
                {label:'账单运费', prop: 'freight', visible: true },
                {label:'预估运费', prop: 'estimate_freight', visible: true },
                {label:'账单重量', prop: 'freight_weight', visible: true },
                {label:'预估重量', prop: 'estimate_weight', visible: true },
                {label:'争议状态', prop: 'disputeapi_status', visible: true },
                {label:'创建时间', prop: 'createtime', visible: true },
            ]
        }
    },
    async created() {
        this.loading = true
        try {
            const result = await getShopList()
            this.shopList = result?.data?.list ?? []
        } catch (error) {
            console.log(error)
            this.loading = false
        }
        this.getList()
    },
    methods: {
        showColumn(prop) {
            const data = this.columns.find(c => c.prop === prop)
            return data?.visible ?? true
        },
        handleExport() {
            this.download = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            exportOrderRefund(qyparams)
                .then(res => {
                    console.log(res)
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    console.log(err)
                    this.download = false
                })
        },
        showFeedbackDialog() {
            this.feedbackDialog.visible = true
            this.feedbackDialog.data = this.queryParams
        },
        showDetailDialog(row, type) {
            this.detailDialog.visible = true
            this.detailDialog.data = row
            this.detailDialog.type = type
        },
        //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            queryOrderRefundList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
